package io.renren.modules.generator.controller;

import io.jsonwebtoken.Claims;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.interceptor.AuthorizationInterceptor;
import io.renren.modules.app.utils.JwtUtils;
import io.renren.modules.generator.entity.TbMovieEntity;
import io.renren.modules.generator.service.TbMovieService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @ClassName : MovieController
 * @Description :
 * @Author : Xiaotianyu
 * @Date: 2021-10-13 16:29
 */
@RestController
@RequestMapping("app/movie")
@Api("客户端-电影管理")
public class MovieController {

    @Autowired
    private TbMovieService tbMovieService;
    @Autowired
    JwtUtils jwtUtils;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("分页条件查询")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tbMovieService.queryPage(params);

        return R.ok().put("page", page);
    }

    // 推荐电影
    @GetMapping("/recommend/list")
    @ApiOperation("推荐电影查询")
    public R recommendList(HttpServletRequest request) {

        UserEntity userEntity = new UserEntity();

        String token = request.getHeader("token");

        if (StringUtils.isBlank(token)) {
            return R.ok().put("data", tbMovieService.recommendList(userEntity));
        }

        Claims claimByToken = jwtUtils.getClaimByToken(token);

        String userId = claimByToken.getSubject();

        userEntity.setUserId(Long.valueOf(userId));

        return R.ok().put("data", tbMovieService.recommendList(userEntity));
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("根据ID查询")
    public R info(@PathVariable("id") Integer id) {
        TbMovieEntity tbMovie = tbMovieService.getById(id);

        return R.ok().put("tbMovie", tbMovie);
    }
}
